// 不是签约会员
const isNotSignVip = (accountType) => accountType !== 20

// 有平台活动
const hasPlatActivity = ({ activityPlatform, isActivityBegin }) =>
  (activityPlatform && activityPlatform !== 20) &&
  (isActivityBegin && isActivityBegin !== 1)

// 使用成本价
const useCostPrice = (costPrice) => ({
  disPrice: costPrice, // disPrice 显示的价格
  delPrice: '', // delPrice 划线价，有活动显示售价
  conutPrice: costPrice
})

// 使用活动价
const useActivePrice = ({ activityPrice, salePrice }) => ({
  disPrice: activityPrice, // disPrice 显示的价格
  delPrice: activityPrice, // delPrice 划线价，有活动显示售价
  conutPrice: salePrice
})

// 使用售价
const useSalePrice = ({ salePrice, retailPrice }) => {
  const isPriceSaleeSmallerRetail = salePrice < retailPrice // 售价小于零售价
  const price = {
    conutPrice: salePrice,
    disPrice: salePrice, // 没有活动，直接显示售价
  }
  if (isPriceSaleeSmallerRetail) return { ...price, delPrice: '' }
  return price
}

// 使用会员价格
const useVipPrice = ({ salePrice, retailPrice }) => ({
  ...useSalePrice({ salePrice, retailPrice }),
  delPrice: retailPrice, // 划线价，有活动显示售价
})

// 是特价商品
const isSpecialPriceSku = activityType => activityType == 10

// 是满减商品
const isDiscountSku = activityType => activityType == 20

// 获取sku相关信息
const getRelativeSkuInfo = (item) => {
  return { ...item.skuActivityInfo, ...item.skuPriceInfo }
}

// 获取活动价
const getActivePrice = (skuItem, skuInfo) => {
  let activeItem = { ...skuItem }
  activeItem.activityFlag = true //自定义当前有活动
  const isPriceActiveSmallerCost = skuInfo.activityPrice < skuInfo.costPrice // 活动价小于成本价
  //当活动价小于成本价，直接使用成本价，否则使用活动价
  const price = isPriceActiveSmallerCost ? useCostPrice(activeItem.costPrice) : useActivePrice(activeItem)
  activeItem = { ...activeItem, ...price }
  return activeItem
}

export const getSkuDetails = (skus, accountType) => {
  const skuDetails = skus.map(item => {
    let skuItem = { ...item }
    if (skuItem.appDetail) { // 替换详情数据
      skuItem.appDetail = skuItem.appDetail.replace(/\<img/gi, '<img class="richImg"')
    }
    skuItem.revenueRate = skuItem.revenueRate ? 0.13 : skuItem.revenueRate // 税率
    const skuInfo = getRelativeSkuInfo(skuItem)
    if (isNotSignVip(accountType)) {
      const price = hasPlatActivity(skuInfo) ? getActivePrice(skuItem, skuInfo) : useSalePrice(skuItem)
      skuItem = { ...skuItem, ...price }
    } else {
      skuItem = { ...skuItem, ...useVipPrice(skuItem) }
    }
    skuItem.noTaxPrice = (skuItem.conutPrice / (1 + Number(skuItem.revenueRate))).toFixed(2); // 未税价计算
    const { activityItemLimitNum, unitName, activityType, activityFullStr } = skuInfo
    skuItem.couponTips = ''
    if (isSpecialPriceSku(activityType)) { // 特价商品
      skuItem.couponTips = `前1-${activityItemLimitNum}/${unitName}享受活动价,超出数量以原价计算!`
      if (activityItemLimitNum == 1) {
        skuItem.couponTips = `前1${unitName}享受活动价,超出数量以原价计算!`
      }
      if (activityItemLimitNum == 0) {
        skuItem.couponTips = '活动期间，享受活动价，不限量'
      }
    }
    if (isDiscountSku(activityType)) { // 满减商品
      skuItem.couponTips = activityFullStr
    }
    return skuItem
  })
  return skuDetails
}

export const getNewSkuInfo = (filterSku, accountType) => {
  const newSkuInfo = filterSku.map((item) => { // 规格信息部分数据处理
    item.appDetail = !!item.appDetail ? item.appDetail.replace(/\<img/gi, '<img class="richImg"') : item.appDetail; // 替换详情数据
    item.revenueRate = (item.revenueRate == 0 || !item.revenueRate) ? 0.13 : item.revenueRate // 税率
    const { skuActivityInfo: { activityType, activityItemLimitNum, activityPlatform, isActivityBegin, activityPrice, activityFullStr },
      skuPriceInfo: { costPrice, salePrice, retailPrice }, unitName } = item
    if (accountType !== 20) { // 如果不是签约会员
      if (!!activityPlatform && activityPlatform !== 20 && !!isActivityBegin && isActivityBegin !== 1) {
        item.activityFlag = true //自定义当前有没有活动
        if (activityPrice < costPrice) { //当活动价小于成本价，直接使用成本价
          item.disPrice = costPrice // disPrice 显示的价格
          item.delPrice = '' // delPrice 划线价，有活动显示售价
          item.conutPrice = costPrice
        } else { // 反之直接使用活动价
          item.conutPrice = activityPrice;
          item.disPrice = activityPrice
          item.delPrice = salePrice
        }
      } else { // 没有活动价直接使用售价
        item.activityFlag = false
        item.conutPrice = salePrice;
        item.disPrice = salePrice // 没有活动，直接显示售价
        if (salePrice < retailPrice) {
          item.delPrice = ''
        }
      }
    } else { // 签约会员价格显示
      item.disPrice = salePrice // disPrice 显示的价格
      item.delPrice = retailPrice // delPrice 划线价，有活动显示售价
      if (salePrice < retailPrice) {
        item.delPrice = ''
      }
      item.conutPrice = salePrice
    }
    item.noTaxPrice = (item.conutPrice / (1 + Number(item.revenueRate))).toFixed(2); // 未税价计算
    if (activityType == 10) {
      if (activityItemLimitNum == 1) {
        item.couponTips = `前1${unitName}享受活动价,超出数量以原价计算!`
      } else if (activityItemLimitNum == 0) {
        item.couponTips = '活动期间，享受活动价，不限量'
      } else {
        item.couponTips = `前1-${activityItemLimitNum}/${unitName}享受活动价,超出数量以原价计算!`
      }
    } else if (activityType == 20) {
      if (activityFullStr) {
        item.couponTips = activityFullStr
      } else {
        item.couponTips = ''
      }
    }
    return item
  })
  return newSkuInfo
}
